/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * VentanaIngreso.java
 *
 * Created on 07-ago-2010, 20:47:39
 */

package ventanas;
import logicaClient.*;
import javax.swing.*;
import java.awt.Color;
import logicaClient.ComunicacionClient;
/**
 *
 * @author Daniela
 */
public class VentanaIngreso extends javax.swing.JFrame {

    /** Creates new form VentanaIngreso */
    public VentanaIngreso() {
        initComponents();
        this.VentanaPadre3 = null;
        this.VentanaPadreUsuario = null;

    }

    public void setVentanaPadre2(JFrame VentanaPadre)
    {
        this.VentanaPadre2 = VentanaPadre;
        this.VentanaPadreUsuario = VentanaPadre;
    }

    public void VentanaIngreso(JFrame VentanaPadre3)
    {
        this.VentanaPadre3 = VentanaPadre3;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLNickname = new javax.swing.JLabel();
        jLClave = new javax.swing.JLabel();
        JTFNickname = new javax.swing.JTextField();
        JPFClave = new javax.swing.JPasswordField();
        JBCancelar = new javax.swing.JButton();
        JBAceptar = new javax.swing.JButton();
        jLabel3 = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Ingreso al Sistema");
        setName("VentanaIngreso"); // NOI18N

        jLNickname.setText("NickName:");

        jLClave.setText("Clave:");

        JTFNickname.setName("JTFNickname"); // NOI18N
        JTFNickname.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JTFNicknameActionPerformed(evt);
            }
        });

        JBCancelar.setText("Cancelar");
        JBCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBCancelarActionPerformed(evt);
            }
        });

        JBAceptar.setText("Aceptar");
        JBAceptar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                JBAceptarActionPerformed(evt);
            }
        });

        jLabel3.setForeground(new java.awt.Color(255, 0, 0));
        jLabel3.setName("jLErrorIngreso"); // NOI18N

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addContainerGap()
                        .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                        .addGap(10, 10, 10)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(JBAceptar)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(JBCancelar))
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLNickname, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(JTFNickname, javax.swing.GroupLayout.PREFERRED_SIZE, 170, javax.swing.GroupLayout.PREFERRED_SIZE))
                                .addGroup(layout.createSequentialGroup()
                                    .addComponent(jLClave, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(JPFClave))))))
                .addContainerGap(43, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLNickname)
                    .addComponent(JTFNickname, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLClave)
                    .addComponent(JPFClave, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addComponent(jLabel3, javax.swing.GroupLayout.DEFAULT_SIZE, 22, Short.MAX_VALUE)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(JBCancelar)
                    .addComponent(JBAceptar))
                .addContainerGap())
        );

        jLabel3.getAccessibleContext().setAccessibleName("jLErrorIngreso");

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-328)/2, (screenSize.height-197)/2, 328, 197);
    }// </editor-fold>//GEN-END:initComponents

    private void JBCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JBCancelarActionPerformed
        // TODO add your handling code here:
        this.dispose();
        VentanaSecundaria ventanaSecundaria = new VentanaSecundaria();
        ventanaSecundaria.setVisible(true);

    }//GEN-LAST:event_JBCancelarActionPerformed

    private void JBAceptarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JBAceptarActionPerformed
        // TODO add your handling code here:
//ACEPTARR!!!!! nickname clave
     //Ingreso al sistema de usuarios registrados
      datos ="";

       datos = datos.concat("1:");
       datos = datos.concat(JTFNickname.getText());
       datos = datos.concat(":");
       datos = datos.concat(JPFClave.getText());

        System.out.println("Valore de dato en ingreso a usuarios registrados");
        System.out.println("=>  "+datos+"\n");
        retorno=ComunicacionJSockets.solicitarEnvioDatos(datos);
        
        if (retorno.compareTo("false") == 0 || retorno.compareTo("FALSE") == 0)
        {
            System.out.println("El nickname y la contraseña no son validos");
            jLabel3.setText("El NickName y/o Clave no son correctos");
            jLabel3.setForeground(Color.RED);
        }
        else
        {
             System.out.println("El nickname y la contraseña si son validos");

             String[] dato = retorno.split(":");
             String nombre=""; String apellido=""; 
             nombre = dato[1]; apellido=dato[2];
             int avatarID = Integer.parseInt(dato[3]);
             String avatarPATH = "";
             //System.out.println("\nAvatar Imagen   "+ComunicacionClient.obtenerPathAvatarImagen(avatarID));
             avatarPATH = ComunicacionClient.concatenarPathImagen(ComunicacionClient.obtenerPathAvatarImagen(avatarID));

             System.out.println("Nombre devuelto: "+nombre);
             System.out.println("Apellido devuelto: "+apellido+"\n");
             System.out.println("ID avatar devuelto: "+avatarID+"\n");
             ComunicacionClient.IngresoDatosUsuario(JTFNickname.getText(), JPFClave.getText(), nombre, apellido,avatarID,avatarPATH);
             System.out.println("\nVentana Ingreso!!!!!!!!!\n");
            System.out.println(ComunicacionClient.getLosUsuarios().toString());

            // System.out.println("IDAvatar devuelto: "+dato[3]);



            /*this.setVisible(false);
            VentanaJugada ventanaJugada = new VentanaJugada();
            ventanaJugada.setVisible(true);
            ventanaJugada.setVentanaPadre3(this); */
            this.dispose();
            VentanaUsuario ventanaUsuario = new VentanaUsuario();
            ventanaUsuario.setVisible(true);
            ventanaUsuario.setVentanaPadreIngreso(this);


        }


     /*

        if (ComunicacionClient.verificarCuenta(nick,jPasswordField1) == false)
        {
            System.out.println("El nickname y la contraseña no son validos");
          jLabel3.setText("El NickName y/o Clave no son correctos");
        }
        else
        {
            System.out.println("El nickname y la contraseña si son validos");
            this.setVisible(false);
            VentanaJugada ventanaJugada = new VentanaJugada();
            ventanaJugada.setVisible(true);
            ventanaJugada.setVentanaPadre3(this);
        }*/
    }//GEN-LAST:event_JBAceptarActionPerformed

    private void JTFNicknameActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_JTFNicknameActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_JTFNicknameActionPerformed

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new VentanaIngreso().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton JBAceptar;
    private javax.swing.JButton JBCancelar;
    private javax.swing.JPasswordField JPFClave;
    private javax.swing.JTextField JTFNickname;
    private javax.swing.JLabel jLClave;
    private javax.swing.JLabel jLNickname;
    private javax.swing.JLabel jLabel3;
    // End of variables declaration//GEN-END:variables
    private JFrame VentanaPadre2;
    
    javax.swing.JFrame VentanaPadre3;
    javax.swing.JFrame VentanaPadreUsuario;

    private String datos = "";
    private String retorno = "";


}
